'use strict';

module.exports = app => {
  const { INTEGER, DATE, ENUM } = app.Sequelize;

  const Like = app.model.define('like', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    target_type: {
      type: ENUM('outfit', 'clothing', 'comment'),
      allowNull: false,
      comment: '点赞目标类型',
    },
    target_id: {
      type: INTEGER,
      allowNull: false,
      comment: '目标ID',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
  }, {
    tableName: 'likes',
    timestamps: true,
    paranoid: false,  // 禁用软删除
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '点赞表',
    indexes: [
      {
        unique: true,
        fields: ['user_id', 'target_type', 'target_id'],
      },
    ],
  });

  // 关联关系
  Like.associate = function() {
    // 属于用户
    app.model.Like.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });
  };

  return Like;
};
